package com.aloudmeta.grpc.support;

import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
import io.grpc.netty.NettyServerBuilder;
import io.netty.channel.ChannelOption;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.protocol.grpc.interceptors.GrpcConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;

/**
 * huaixin 2022/3/3 8:07 PM
 */
@Activate(group = {PROVIDER})
public class MetastoreServerGrpcConfigurator implements GrpcConfigurator {

    private static final Logger LOG = LoggerFactory.getLogger(MetastoreServerGrpcConfigurator.class);

    @Override
    public NettyServerBuilder configureServerBuilder(NettyServerBuilder builder, URL url) {
        LOG.info("设置 grpc server 参数");
        builder.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) TimeUnit.SECONDS.toMillis(5));
        CompressorRegistry compressorRegistry = CompressorRegistry.newEmptyInstance();
        compressorRegistry.register(new ZstdCompressor());
        builder.compressorRegistry(compressorRegistry);

        DecompressorRegistry decompressorRegistry = DecompressorRegistry.emptyInstance();
        decompressorRegistry = decompressorRegistry.with(new ZstdDecompressor(), true);

        builder.decompressorRegistry(decompressorRegistry);

        return builder;
    }
}
